package com.youdianstar.app.ui.pay

import android.graphics.Paint
import android.view.View
import com.youdianstar.app.R
import com.youdianstar.app.BR
import com.youdianstar.app.databinding.ActivityRefundResultBinding
import com.youdianstar.app.common.ConfigKey
import com.youdianstar.app.common.base.BaseActivity
import com.youdianstar.app.common.ex.getViewColor
import com.youdianstar.app.common.ex.setThrottleListener
import com.youdianstar.app.common.util.Tool
import com.youdianstar.app.common.util.dialog.DialogUtil

import com.youdianstar.app.util.DataAppManager
import com.youdianstar.app.common.util.StrUtil
import org.koin.androidx.viewmodel.ext.android.viewModel


/**
 * File: RefundStateActivity
 * Author: 82149 Create: 2021/11/25 20:02
 * Changes (from 2021/11/25)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class RefundStateActivity :BaseActivity<ActivityRefundResultBinding, WalletViewModel>(){
    override fun layoutId(): Int = R.layout.activity_refund_result

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): WalletViewModel {
        val model: WalletViewModel by viewModel()
        return model
    }

    private var whatsNum : String?=null
//    private var phoneNum : String? =null
    private var emailStr : String? =null

    override fun initViewLayout() {
        super.initViewLayout()
        mBinding.headBar.initTitle(this,title = "", statusColor = R.color.white, isBackFinish = false)
        mBinding.headBar.setBackUnable(false)

        whatsNum = DataAppManager.instance.getConfig(ConfigKey.WhatsApp)
//        phoneNum = DataAppManager.instance.getConfig(ConfigKey.PHONE)
        emailStr = DataAppManager.instance.getConfig(ConfigKey.EMAIL)

        whatsNum?.let {
            val paintPhone = Paint()
            paintPhone.color = getViewColor(this,R.color.color_default)
            paintPhone.flags = Paint.UNDERLINE_TEXT_FLAG
            mBinding.tvPhoneNum.paintFlags = paintPhone.flags
            mBinding.tvPhoneNum.text = it
        }
//        emailStr?.let {
//            val paintEmail = Paint()
//            paintEmail.color = getViewColor(this,R.color.color_default)
//            paintEmail.flags = Paint.UNDERLINE_TEXT_FLAG
//            mBinding.tvEmailNum.paintFlags = paintEmail.flags
//            mBinding.tvEmailNum.text = it
//        }
        if (StrUtil.isEmpty(emailStr)){
            mBinding.tvEmailNum.text = "--"
            mBinding.ivCopy.visibility = View.INVISIBLE
        }else{
            mBinding.tvEmailNum.text = emailStr
            mBinding.ivCopy.visibility = View.VISIBLE
        }

        mBinding.ivResult.setImageResource(R.mipmap.ic_order_rp_success)
    }

    override fun initClickEvent() {
        mBinding.btnSubmit.setThrottleListener {
            finish()
        }

        mBinding.tvPhoneNum.setThrottleListener {
            callWhatsapp()
        }

        mBinding.tvEmailNum.setThrottleListener {
//            emailStr?.let { address->
//                val emailCall = String.format(getString(R.string.Dialog_SelectEmail_Message),address)
//                showActionDialog(emailCall) {
//                    Tool.callEmail(this@RefundStateActivity,address)
//                }
//            }
        }

        mBinding.ivCopy.setThrottleListener {
            if (!StrUtil.isEmpty(emailStr)){
                Tool.copy(this, emailStr)
                toastShow(getString(R.string.invite_copied))
            }
        }
    }

    private fun callWhatsapp(){
        whatsNum?.let { num ->
            val appCall = getString(R.string.Dialog_SelectWhatsapp_Message)
            showActionDialog(appCall) {
                Tool.callWhatsApp(this,num)
            }
        }
    }

    private fun showActionDialog(msg :String, confirmAction: (() -> Unit) = { }){
        DialogUtil.instance.showNotifyDialog(this,
            title = getString(R.string.public_notice),
            msg = msg,leftAction = {

            },left = getString(R.string.public_cancel),
            rightAction = confirmAction,
            right = getString(R.string.home_confirm),
            isShowLeft = true,isShowRight = true)
    }
}